package com.eco.fanliapp.controller.app;

import com.alibaba.fastjson.JSONObject;
import com.eco.fanliapp.common.base.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.AppUser;
import com.eco.fanliapp.entity.AppWithdrawal;
import com.eco.fanliapp.service.IAppUserService;
import com.eco.fanliapp.service.IAppWithdrawalService;
import com.eco.fanliapp.vo.AppUserFansTeamVo;
import com.eco.fanliapp.vo.respone.ResponeOrderResults.OrderDataBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

@SuppressWarnings("rawtypes")
@Controller
@RequestMapping("/use")
public class UserInfoController extends BaseController {

	@Autowired
	private IAppUserService iAppUserService;
	@Autowired
	private IAppWithdrawalService iAppWithdrawalService;

	@Autowired
	RedisTemplate redisTemplate;
	
	/**
	 * 首先查询用户有多少可提现余额-展示为我的收益的4个值-used
	 * @return
	 */
	@GetMapping("/zfb/getbalance")
	@ResponseBody
	public ServerResponse<JSONObject> getbalance(){
		AppUser u = getAppUser();
		if (u == null || u.getUserId() == null) {
			return ServerResponse.createByErrorCodeMessage(301, "Token不正确");
		}
		return iAppUserService.findAppUserBybalance(u.getUserId());
	}
	
	
	//未绑定支付宝时--旧
	@GetMapping("/zfb/addZfbAccount")
	@ResponseBody
	public ServerResponse<JSONObject> addZfbAccount(String zname, String znumber, String phoneNumber){
		AppUser u = getAppUser();
		if(null == u) {
			return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
		}
		return iAppUserService.addAppUserAlipay(u.getUserId(),zname,znumber,phoneNumber);
	}

	//没有绑定手机号，第一次绑定支付宝时--(2.1.0)
	@GetMapping("/zfb/addAlipayAccount")
	@ResponseBody
	public ServerResponse<JSONObject> addAlipayAccount(String zname, String znumber, String phoneNumber, String code){

		/*if(phoneNumber == null && "".equals(phoneNumber)){
			return iAppUserService.addAppUserAlipay(u.getUserId(),zname,znumber,phoneNumber);
		}*/
		AppUser u = getAppUser();
		if(null == u) {
			return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
		}
		AppUser user = iAppUserService.selectAppUserByAlipayPhoneNumber(phoneNumber);
		if(user!=null){
			return ServerResponse.createByErrorMessage("手机号已绑定，请更换手机号");
		}
		String verifyCode = (String) redisTemplate.opsForValue().get(phoneNumber);
		verifyCode = verifyCode == null?"":verifyCode;
		if (!verifyCode.equals(code)) {
			return ServerResponse.createByErrorMessage("验证码错误");
		}
			return iAppUserService.addAppUserAlipay(u.getUserId(),zname,znumber,phoneNumber);
	}

	//绑定后，手改支付宝账号
	@GetMapping("/zfb/updateZfbAccount")
	@ResponseBody
	public ServerResponse<JSONObject> updateZfbAccount(String zname, String znumber, String phoneNumber){
		AppUser u = getAppUser();
		if(null == u) {
			return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
		}

		return iAppUserService.updateAppUserAlipay(u.getUserId(),zname,znumber,phoneNumber);
	}

	//绑定手机号--（支付宝手机号）(2.1.0)
	@GetMapping("/phone/addphoneNumber")
	@ResponseBody
	public ServerResponse<JSONObject> addphoneNumber(String phoneNumber, String code){


		AppUser u = getAppUser();
		if(null == u) {
			return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
		}
		AppUser user = iAppUserService.selectAppUserByAlipayPhoneNumber(phoneNumber);
		if(user!=null){
			return ServerResponse.createByErrorMessage("手机号已绑定，请更换手机号");
		}
		return iAppUserService.addAlipayPhoneNumber(u.getUserId(),phoneNumber,code);
	}

	//提现记录新增
	@GetMapping("/zfb/addwithdrawalsRecord")
	@ResponseBody
	public ServerResponse<List<JSONObject>> withdrawalsRecord(String money){
		AppUser u = getAppUser();
		if(null == u) {
			return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
		}
		return iAppWithdrawalService.addAppCashwithdrawal(money,u.getUserId());
	}
	
	//提现记录查询
	@GetMapping("/zfb/findwithdrawalsRecord")
	@ResponseBody
	public ServerResponse<List<AppWithdrawal>> selectwithdrawalsRecord(Integer pageNum, Integer pageSize){
		AppUser u = getAppUser();
		if(null == u) {
			return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
		}
		return iAppWithdrawalService.findAppWithdrawalByAll(u.getUserId(),pageNum,pageSize);
	}
	
	//我的团队 --旧
	@GetMapping("/user/findMeTeam")
	@ResponseBody
	public ServerResponse<Map<String, Object>> TeamInvitation(){
		AppUser u = getAppUser();
		if(null == u) {
			return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
		}
		return iAppUserService.teamAll(u.getUserId());
	}
	//

	/**
	 * 我的团队 --粉丝人数 粉丝贡献--used
	 * @return
	 */
	@GetMapping("/user/findMyTeam")
	@ResponseBody
	public ServerResponse<Map<String, Object>> teamFans(){
		AppUser u = getAppUser();
		if (null == u || u.getUserId() == null) {
			return ServerResponse.createByErrorCodeMessage(301, "Token不正确");
		}
		return iAppUserService.teamFans(u.getUserId());
	}

	//我的团队-->>我的下级-旧
	@GetMapping("/user/findMeTeamList")
	@ResponseBody
	public ServerResponse<List<Map<String, Object>>> TeamInvitationList(String name, Integer pageNum, Integer pageSize){
		AppUser u = getAppUser();
		if(null == u) {
			return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
		}
		return iAppUserService.teamOrderDetail(u.getUserId(),name,pageNum,pageSize);
	}

	/**
	 * 我的团队-->>我的下级--used
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@GetMapping("/user/findFansTeamList")
	@ResponseBody
	public ServerResponse<List<AppUserFansTeamVo>> findFansTeamList(Integer pageNum, Integer pageSize){
		AppUser u = getAppUser();
		if(null == u || u.getUserId() == null) {
			return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
		}
		return iAppUserService.findFansTeamList(u.getUserId(),pageNum,pageSize);
	}
	
	//粉丝贡献 订单--》我的下级--》我的下级的下级
	@GetMapping("/user/myTeamOrders")
	@ResponseBody
	public ServerResponse<List<OrderDataBean>> myTeamOrder(Integer tkStatus, Integer pageNum, Integer pageSize){
		AppUser u = getAppUser();
		if(null == u) {
			return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
		}
		return iAppUserService.TeamOrder(u.getUserId(),tkStatus,pageNum,pageSize);
	}
	
	//累计收益 --旧
	@GetMapping("/user/income")
	@ResponseBody
	public ServerResponse<Map<String, Object>> myIncome(){
		AppUser u = getAppUser();
		if(null == u) {
			return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
		}
		return iAppUserService.findbalanceIncome(u.getUserId());
	}

	/**
	 * 累计补贴-查询 订单补贴 粉丝贡献 订单数量，总收益-used
	 * @return
	 */
	@GetMapping("/user/subsidies")
	@ResponseBody
	public ServerResponse<Map<String, Object>> mySubsidies(){
		AppUser u = getAppUser();
		if (null == u || u.getUserId() == null) {
			return ServerResponse.createByErrorCodeMessage(301, "Token不正确");
		}
		return iAppUserService.findBalanceIncome(u.getUserId());
	}
	
	//查询我的补贴-使用
	@GetMapping("/user/myAllowance")
	@ResponseBody
	public ServerResponse myAllowance() {
		AppUser u = getAppUser();
		if(null == u) {
			return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
		}
		return iAppUserService.findAllowance(u.getUserId());
	}
	
	//助力红包
	@GetMapping("/user/myFenSiHb")
	@ResponseBody
	public ServerResponse myFenSiHb() {
		AppUser u = getAppUser();
		if(null == u) {
			return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
		}
		return iAppUserService.findInvitationFenSiCount(u.getUserId());
	}

	//邀请赚钱
	@GetMapping("/user/makeMoney")
	@ResponseBody
	public ServerResponse invitationMakeMoney(){
		AppUser u = getAppUser();
		if(null == u) {
			return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
		}
		return iAppUserService.invitationMakeMoney(u.getUserId());
	}

	//邀请赚钱可提现
	@GetMapping("/user/makeMoneyCash")
	@ResponseBody
	public ServerResponse invitationMakeMoneyCash(){
		AppUser u = getAppUser();
		if(null == u) {
			return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
		}
		return iAppUserService.invitationMakeMoneyCash(u.getUserId());
	}


	//没有绑定手机号，第一次绑定支付宝时--新
	@PostMapping("/zfb/alipayAccount/PhoneNumber")
	@ResponseBody
	public ServerResponse insertAlipayAccountAndPhoneNumber(String zname, String znumber, String phoneNumber, String code){

		AppUser u = getAppUser();
		if(null == u) {
			return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
		}
		AppUser user = iAppUserService.selectAppUserByAlipayPhoneNumber(phoneNumber);
		if(user!=null){
			return ServerResponse.createByErrorMessage("手机号已绑定，请更换手机号");
		}
		String verifyCode = (String) redisTemplate.opsForValue().get(phoneNumber);
		verifyCode = verifyCode == null?"":verifyCode;
		if (!verifyCode.equals(code)) {
			return ServerResponse.createByErrorMessage("验证码错误");
		}
		return iAppUserService.insertAlipayPhoneNumber(u.getUserId(),zname,znumber,phoneNumber);
	}

	//支付宝提现，实时到账
	@GetMapping("/zfb/toAliPay")
	@ResponseBody
	public ServerResponse<List<JSONObject>> withdrawalsRecordToAliPay(String money){
		Calendar now = Calendar.getInstance();
		Integer month = (now.get(Calendar.MONTH) + 1);
		Integer day = now.get(Calendar.DAY_OF_MONTH);

		if(month == 1 && day > 21 && day < 31){
			return ServerResponse.createByErrorMessage("温馨提示：春节假期期间暂停提现，敬请谅解。");
		}
		AppUser u = getAppUser();
		if(null == u) {
			return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
		}
		return iAppWithdrawalService.withdrawalsRecordToAliPay(money,u.getUserId());
	}
}
